import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/entity/order/detail/order_detail_entity.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/provider/order/order_list.dart';
import 'package:distribution_mall_flutter/router/router.gr.dart';
import 'package:distribution_mall_flutter/utils/utils.dart';
import 'package:distribution_mall_flutter/widgets/card.dart';
import 'package:distribution_mall_flutter/widgets/goods_item_tile.dart';
import 'package:distribution_mall_flutter/widgets/ink_well_view.dart';
import 'package:distribution_mall_flutter/widgets/navigation_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class GoodsCard extends ConsumerWidget {
  final OrderItemGroupList data;
  final double? height;
  const GoodsCard({super.key, required this.data, this.height});

  Widget skuRender(OrderGoodsList item) {
    return Column(
      children: [
        EPInkWellView(
          onPressed: () {
            appRouter
                .push(ProductDetailsRoute(saleItemId: item.saleItemId ?? ''));
          },
          width: double.infinity,
          child: Container(
            width: double.infinity,
            height: 75,
            padding: const EdgeInsets.all(EdgeConstant.edge10),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(RadiusConstant.edge5),
                color: ColorConstant.grey246),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      item.spec ?? '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(
                          fontSize: FontConstant.f11, color: Colors.black),
                    ),
                    Text(
                      item.specUnit?.split('of').last.trim() ?? '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(
                        fontSize: FontConstant.f11,
                        color: ColorConstant.grey136,
                      ),
                    ),
                    Utils.currencyWidget(
                      item.subTotalAtOrderCurrency,
                      label: '${S.current.total}:',
                      commonStyle: const TextStyle(
                          color: ColorConstant.grey136,
                          fontSize: FontConstant.f10),
                    )
                  ],
                )),
                Padding(
                  padding: const EdgeInsets.only(left: EdgeConstant.edge10),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text(
                        '${item.salePriceAtOrderCurrency}',
                        style: const TextStyle(
                            fontSize: FontConstant.f13,
                            color: ColorConstant.black51,
                            fontWeight: FontWeight.bold),
                      ),
                      Text(
                        'x${item.quantity}',
                        style: const TextStyle(
                          fontSize: FontConstant.f13,
                          color: ColorConstant.grey136,
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
        const SizedBox(height: 10),
      ],
    );
  }

  Widget sekectItemRender(
      {Function()? onTap,
      String leftText = '',
      String rightText = '',
      bool showRightIcon = false}) {
    return EPNavigationItem(
      isLineNav: true,
      leftText: leftText,
      rightText: rightText,
      showRightIcon: showRightIcon,
      leftTextStyle: const TextStyle(
          color: ColorConstant.grey153, fontSize: FontConstant.f14),
      rightTextStyle: const TextStyle(
          color: ColorConstant.black51, fontSize: FontConstant.f14),
      padding: const EdgeInsets.symmetric(
        horizontal: EdgeConstant.edge10,
        vertical: EdgeConstant.edge8,
      ),
      onTap: onTap,
    );
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return EPCard(
      height: height,
      margin: const EdgeInsets.fromLTRB(
          EdgeConstant.edge10, EdgeConstant.edge10, EdgeConstant.edge10, 0),
      padding: const EdgeInsets.only(
          top: EdgeConstant.edge15, bottom: EdgeConstant.edge5),
      child: Column(
          children: data.orderSuItemList
                  ?.map((item) => Column(children: [
                        Padding(
                            padding: const EdgeInsets.symmetric(
                                horizontal: EdgeConstant.edge10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: EdgeConstant.edge10),
                                  child: EPGoodsItemTile(
                                      textAxisAlign: MainAxisAlignment.start,
                                      data: EPGoodsItemTileransform.fromJson({
                                        'mainImage': item.mainImage,
                                        'title': item.title
                                      })),
                                ),
                                ...(item.orderGoodsList
                                        ?.map((v) => skuRender(v))
                                        .toList() ??
                                    []),
                                const Divider(
                                  color: ColorConstant.grey221,
                                )
                              ],
                            )),
                        sekectItemRender(
                            leftText: S.of(context).logistics,
                            rightText: ref
                                .read(orderListProvider.notifier)
                                .getTransportName(data.transportType)),
                        // onTap: () => showCupertinoModalBottomSheet(
                        // context: context,
                        // builder: (context) => const ModalLogistics(),
                        // barrierColor: ColorConstant.blackOp4)
                        // sekectItemRender(
                        //     leftText: S.of(context).totalProducts,
                        //     onTap: () {}),
                        //   sekectItemRender(
                        //       leftText: S.of(context).note, onTap: () {}),
                        // ],
                      ]))
                  .toList() ??
              []),
    );
  }
}
